buyfollowMgr = {}
QMPlugin = buyfollowMgr
function buyfollowMgr.init()
    buyfollowMgr.followMode = ""
    if boot.platform == 1 then
        buyfollowMgr.paraConfig = {
            ["关注按钮已选中"] = { 320,68,916,446,"3862ED","1|-37|6696DC,-2|65|182971,12|86|182971,-2|103|182971,-14|106|182971",0,0.9},
            ["关注标签"] = { 149,467,909,1765,"6C54EE","-14|-18|D6EBFB,-10|-28|D1E1F5,6|-14|CCE3F9,14|17|7459FE,-29|-26|7459FE,3|-20|755BFF,2|8|D3EAF8,5|12|C2E1F4,-14|-9|D6ECFA",0,0.9},
            ["关注宠物梦幻币ICON"] = {135,468,906,1791,"88E1FF","2|-12|34A8E8,21|2|E9FFFF,2|22|40B8F1,-19|10|41C2F8,7|9|63E8FF,-9|13|2C9DE0,7|-7|2793D7",4,0.95},
            ["珍品标签"] = { 837,640,948,738,"FA71AB","-18|-22|FA71AB,27|22|FA71AB,18|-14|FB70AB,7|-21|D5E9FC,6|6|D3E8F4,26|6|C6CDE0,-3|-12|D1E6FB",0,0.9},
            ["关闭按钮"] = { 805,1719,959,1849,"92DCFF","18|-16|7DC8FF,-17|17|9EE5FE,-13|-16|A5EFFF,-26|2|4C5496,29|1|445496,2|-26|425294,1|34|475392",0,0.9},
            ["抢购点击点"] = {  124,1666,297,1835,"C4E8FC","-25|110|8D9BFD,-11|114|A8A0FE,1|129|ACCCF1,-85|100|9E8BFD,-63|98|A5D2F0,3|-80|CEEDFF",0,0.9},
            ["购买按钮"] = { 54,1168,1073,1904,"4E58D8","13|-46|D0EEFF,-13|-21|CFEEFF,8|16|D0EEFF,17|45|D0EEFF,-12|43|CFEEFF,-4|75|4C56D6,24|0|5F67E8,19|18|D0EEFF,-24|-3|454FDF",0,0.9},
        }
    else
        buyfollowMgr.paraConfig = {
            ["关注标签"] = { 491,335,641,449,"7459FE","16|15|6952E9,22|8|6F59F6,31|31|7459FE,14|4|C8DAF0,30|18|D6EBFD,22|27|C2D5EC,12|9|D6E9FA,12|17|C9E7EF,25|11|C0D5EB",0,0.9},
            ["购买按钮"] = { 100,861,628,1269,"4F58D9","-2|-67|4E58D7,-16|-1|464FDE,1|60|505ADA,17|-5|6169E9,4|-33|ABE1FF,-9|-25|ABE1FF,-2|13|ABE1FF,4|21|ABE1FF,-9|29|ABE1FF",0,0.9},
            ["关闭按钮"] = {  66,845,646,1274,"8CD2FB","2|-21|3A477D,4|13|3C4074,-13|-3|3E4073,19|-2|3A487F,12|-16|72B9FB,-10|-15|A1E8FE,-11|8|A1E9FF,13|8|71B6FB,-4|-8|94DBFC",0,0.9},
            ["珍品标签"] = { 395,869,533,997,"F474A9","4|-5|F871AD,-23|-11|F972AC,13|24|F972AC,5|9|D0E6F7,-11|-10|D2E7FD,-12|0|CFE3F4,3|11|D2E0EE",0,0.9},
            ["关注标签mask"] = { 503,324,637,435,"2F2466","7|-8|2A215D,-21|-16|2E2466,13|22|342F66,14|2|535B63,-2|-10|4E555F,-10|-7|515963,5|11|4E555F,16|5|4E535C,8|-4|4F5661",0,0.9},
            ["界面关闭按钮"] = { 640,1190,749,1328,"87CCFD","10|-15|75BBFA,-13|9|9EE5FE,-13|-17|9EE5FE,12|11|73B8FA,-3|-34|394178,-31|-2|484387,0|22|414381,23|1|343E79,-5|-23|333E7B",0,0.95},
        }
    end
end
function buyfollowMgr.setFollowMode(mode)
    buyfollowMgr.followMode = mode
end

local function checkBuyBtn()
    local para = buyfollowMgr.paraConfig["购买按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function checkCloseBtn()
    local para = buyfollowMgr.paraConfig["关闭按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function checkFollowSelected()
    local para = buyfollowMgr.paraConfig["关注按钮已选中"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function checkPanelCloseBtn()
    return game.GetCloseBtn("摆摊关闭按钮")
end

local function checkFollowFlag()
    local para = buyfollowMgr.paraConfig["关注标签"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        para = buyfollowMgr.paraConfig["关注标签mask"]
        itemX,itemY = game.findMultiColor(utils.unpack(para))
        if itemX == -1 or itemY == - 1 then
            return false,itemX,itemY
        else
            return true,itemX,itemY
        end
    else
        return true,itemX,itemY
    end
end

local function checkZhengpingFlag()
    local para = buyfollowMgr.paraConfig["珍品标签"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function checkMhIconFlag()
    local para = buyfollowMgr.paraConfig["关注宠物梦幻币ICON"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function checkBuyPoint()
    local para = buyfollowMgr.paraConfig["抢购点击点"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function checkCloseBtn()
    return game.GetCloseBtn("摆摊宠物关闭按钮")
end


local function isTimeDisplayDead()
    local isDead = zm.IsDisplayDead(411,1508,488,1730, 2000)
    if isDead then
        mlog.info("卡屏了")
        return true
    else
        mlog.info("没卡屏")
        return false
    end
end

local function normalMode()
    LuaAuxLib.TracePrint("关注抢购 execTask")
    LuaAuxLib.TracePrint("关闭其他面板")
    game.OtherPanelClose()
    game.delay(500)
    LuaAuxLib.TracePrint("打开关注面板")
    game.openFollowPanel()
    game.delay(500)
    while true do
        local exsits = checkFollowFlag()
        if exsits then
            LuaAuxLib.TracePrint("发现有抢购物品")
            local iconFlag,x,y = checkMhIconFlag()
            if iconFlag then
                game.tap(x,y)
                game.delay(1500)
            end

            if isTimeDisplayDead() then
                LuaAuxLib.TracePrint("没有开始倒计时")
                local closeBtnShow,x,y = checkCloseBtn()
                if closeBtnShow then
                    game.tap(x,y)
                    game.delay(1000)
                end
            else
                LuaAuxLib.TracePrint("开始倒计时")
                local flagExsits = checkZhengpingFlag()
                mlog.info("珍品标签 = "..tostring(flagExsits))
                if flagExsits then
                    mlog.info("点出抢购界面")
                    local pointFlag,x,y = checkBuyPoint()
                    while flagExsits do
                        game.tap(x,y)
                    end
                    mlog.info("抢购完成")
                    game.delay(500)
                    local followBtn,x,y = checkFollowSelected()
                    if followBtn then
                        game.tap(x,y)
                        game.delay(500)
                    end
                end
            end
        else
            local closeBtnExsits,x,y = checkPanelCloseBtn()
            if closeBtnExsits then
                mlog.info("抢购结束")
                game.tap(x,y)
                break
            end
        end
    end
end

function buyfollowMgr.execTask()
    normalMode()
end

